package pers.xx.sbm.utils.file;

import org.springframework.util.StringUtils;
import pers.xx.sbm.dao.MatchErrorLogMapper;
import pers.xx.sbm.entity.BaseArea;
import pers.xx.sbm.entity.BaseSchool;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.List;

/**
 * @author ：xiexing
 * @description：TODO
 * @date ：2021/1/27 22:25
 */
public class FileMoveUtils {


    private MatchErrorLogMapper matchErrorLogMapper;

    public FileMoveUtils(MatchErrorLogMapper matchErrorLogMapper) {
        this.matchErrorLogMapper = matchErrorLogMapper;
    }

    /**
     * @description 文件移动 根据区县名称创建文件夹 并将文件夹下的学校简介移动到对应区县文件夹
     * @author XieXing
     * @date 2021/1/27 22:25
     */
    public void move(BaseArea baseArea, List<BaseSchool> baseSchools) {

        //创建区县文件夹
        String path = "C:\\Users\\18359\\Desktop\\学校简介（按区县划分，按公民办划分）";
        path += (File.separator + baseArea.getName());

        File dir = new File(path);
        if (!dir.isDirectory()) {
            dir.mkdir();
        }
        //在区县下面创建公民办文件夹
        String gbPath = path + File.separator + "公办";
        File gbDir = new File(gbPath);
        if (!gbDir.isDirectory()) {
            gbDir.mkdir();
        }
        String mbPath = path + File.separator + "民办";
        File mbDir = new File(mbPath);
        if (!mbDir.isDirectory()) {
            mbDir.mkdir();
        }
        //遍历学校简介并移动到该区县下

        String sourcePath = "C:\\Users\\18359\\Desktop\\school\\";
        String tmpPath = "";
        for (BaseSchool baseSchool : baseSchools) {
            if (StringUtils.isEmpty(baseSchool.getAnnexOss())) {//该学校未对应招生简章
                addMatchLog(baseArea, baseSchool, "学校未对应招生简章");
            } else { //该学校对用招生简章
                tmpPath = sourcePath + baseSchool.getAnnexOss();
                File sourceFile = new File(tmpPath);
                if (sourceFile.exists()) { //文件存在 移动到区县文件夹下
                    if (baseSchool.getType() == 1) {
                        fileCopy(tmpPath, gbPath, baseSchool.getName()+"["+baseSchool.getSchoolTypeName()+"]");
                    } else {
                        fileCopy(tmpPath, mbPath, baseSchool.getName()+"["+baseSchool.getSchoolTypeName()+"]");
                    }
                } else {//文件不存在
                    addMatchLog(baseArea, baseSchool, "学校招生简章文件未找到");
                }
            }
        }
    }


    /**
     * @description 文件复制 暂时不用静态的
     * @author XieXing
     * @date 2021/1/27 22:41
     */
    private void fileCopy(String srcPathStr, String desPathStr, String sourceFileName) {
        //获取源文件的名称
        desPathStr = desPathStr + File.separator + sourceFileName+".pdf"; //目标文件地址
        try {
            FileInputStream fis = new FileInputStream(srcPathStr);//创建输入流对象
            FileOutputStream fos = new FileOutputStream(desPathStr); //创建输出流对象
            byte datas[] = new byte[1024 * 8];//创建搬运工具
            int len = 0;//创建长度
            while ((len = fis.read(datas)) != -1)//循环读取数据
            {
                fos.write(datas, 0, len);
            }
            fis.close();//释放资源
            fis.close();//释放资源
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    /**
     * @description 添加匹配日志
     * @author XieXing
     * @date 2021/1/27 22:59
     */
    private void addMatchLog(BaseArea baseArea, BaseSchool baseSchool, String matchResult) {
//        MatchErrorLog matchErrorLog = new MatchErrorLog();
//        matchErrorLog.setCountName(baseArea.getName());
//        matchErrorLog.setCountyId(baseArea.getId());
//        matchErrorLog.setSchoolId(baseSchool.getId());
//        matchErrorLog.setSchoolName(baseSchool.getName());
//        matchErrorLog.setMatchTime(new Date());
//        matchErrorLog.setMatchResult(matchResult);
//        matchErrorLog.setIntroduceFileName(baseSchool.getAnnexOss());
//        matchErrorLog.setRemark(baseSchool.getType() + "");
//        matchErrorLogMapper.insert(matchErrorLog);
        System.out.println("错误学校：" + baseSchool.getName());
    }
}
